package com.neusoft.neuiotms.dcom.converter;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.MonthWeeklyDTO;
import com.neusoft.neuiotms.dcom.model.MonthWeekly;
import com.neusoft.neuiotms.dcom.repository.MonthWeeklyRepository;

@Component
public class MonthWeeklyConverter extends AbstractConverter<MonthWeekly, MonthWeeklyDTO> {

    @Autowired
    private MonthWeeklyRepository monthWeeklyRepository;

    @Override
    public MonthWeekly toModel(final MonthWeeklyDTO dto) {

        MonthWeekly model;
        if (dto.isNew()) {
            model = new MonthWeekly();
        } else {
            model = this.monthWeeklyRepository.getOne(dto.getId());
        }

        model.setProjectName(dto.getProjectName());
        model.setClientCode(dto.getClientCode());
        model.setClientName(dto.getClientName());
        model.setOperationUserName(dto.getOperationUserName());
        model.setOperationUserCode(dto.getOperationUserCode());
        model.setResidentEngineerUserName(dto.getResidentEngineerUserName());
        model.setResidentEngineerUserCode(dto.getResidentEngineerUserCode());
        model.setMobileEngineerUserName(dto.getMobileEngineerUserName());
        model.setMobileEngineerUserCode(dto.getMobileEngineerUserCode());
        model.setServiceMonth(dto.getServiceMonth());
        model.setUnplannedWanOutageDuration(dto.getUnplannedWanOutageDuration());
        model.setWanMonthlyAvailability(dto.getWanMonthlyAvailability());
        model.setUnplannedLanOutageDuration(dto.getUnplannedLanOutageDuration());
        model.setLanMonthlyAvailability(dto.getLanMonthlyAvailability());
        model.setUnplannedExtranetOutageDuration(dto.getUnplannedExtranetOutageDuration());
        model.setExtranetMonthlyAvailability(dto.getExtranetMonthlyAvailability());
        model.setResidentServiceTotalTime(dto.getResidentServiceTotalTime());
        model.setOperationTime(this.formatMonthDate(dto.getOperationTime()));
        model.setWeeklySummary(dto.getWeeklySummary());
        model.setMonthSummary(dto.getMonthSummary());
        model.setCustomerSatisfaction(dto.getCustomerSatisfaction());
        model.setMajorIncidentReport(dto.getMajorIncidentReport());
        model.setNextMonthWorkPlan(dto.getNextMonthWorkPlan());
        model.setReasonableSuggestion(dto.getReasonableSuggestion());
        model.setRemark(dto.getRemark());
        model.setServiceTotalNumber(dto.getServiceTotalNumber());
        model.setServiceTotalTime(dto.getServiceTotalTime());
        model.setAttachment(dto.getAttachment());

        return model;
    }

    @Override
    public MonthWeeklyDTO toDTO(final MonthWeekly model, final boolean forListView) {

        final MonthWeeklyDTO dto = new MonthWeeklyDTO();
        dto.setId(model.getId());
        dto.setProjectName(model.getProjectName());
        dto.setClientName(model.getClientName());
        dto.setClientCode(model.getClientCode());
        dto.setOperationUserName(model.getOperationUserName());
        dto.setOperationUserCode(model.getOperationUserCode());
        dto.setResidentEngineerUserName(model.getResidentEngineerUserName());
        dto.setResidentEngineerUserCode(model.getResidentEngineerUserCode());
        dto.setMobileEngineerUserName(model.getMobileEngineerUserName());
        dto.setMobileEngineerUserCode(model.getMobileEngineerUserCode());
        dto.setServiceMonth(model.getServiceMonth());
        dto.setUnplannedWanOutageDuration(model.getUnplannedWanOutageDuration());
        dto.setWanMonthlyAvailability(model.getWanMonthlyAvailability());
        dto.setUnplannedLanOutageDuration(model.getUnplannedLanOutageDuration());
        dto.setLanMonthlyAvailability(model.getLanMonthlyAvailability());
        dto.setUnplannedExtranetOutageDuration(model.getUnplannedExtranetOutageDuration());
        dto.setExtranetMonthlyAvailability(model.getExtranetMonthlyAvailability());
        dto.setResidentServiceTotalTime(model.getResidentServiceTotalTime());
        dto.setOperationTime(model.getOperationTime());
        dto.setWeeklySummary(model.getWeeklySummary());
        dto.setMonthSummary(model.getMonthSummary());
        dto.setCustomerSatisfaction(model.getCustomerSatisfaction());
        dto.setMajorIncidentReport(model.getMajorIncidentReport());
        dto.setNextMonthWorkPlan(model.getNextMonthWorkPlan());
        dto.setReasonableSuggestion(model.getReasonableSuggestion());
        dto.setRemark(model.getRemark());
        dto.setServiceTotalNumber(model.getServiceTotalNumber());
        dto.setServiceTotalTime(model.getServiceTotalTime());
        dto.setAttachment(model.getAttachment());
        return dto;
    }

    private Date formatMonthDate(Date value) {
        if (null == value) {
            value = new Date();
        }
        final Calendar c = Calendar.getInstance();
        c.setTime(value);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return value;
    }

}
